$(function(){
	var inputTip = function(tipText){
		$('.inputTip').show().text(tipText);
		setTimeout(function () {
            $('.inputTip').hide()
        },3000);
		$(":text").focus(function(){
			$('.inputTip').hide()
		})
	}
	
	//获取国家编码
	$.post("http://www.fashionfinger.com/ssfk/global/getCountryCode", function(data) {
		if(0 == data.status) {
			var countryInfo = data.data.letter_country_code;
			var i, j, brands, brandItem, brandItemDom, classifyDom, firstKeySet, tempId, hot;
			var brandPopUl = $(".brand-pop-dom");
			var brandLetter = $(".brand-letter");
			for(i in countryInfo) {
				brands = countryInfo[i];
				firstKeySet = false;
				classifyDom = "";
				for(j in brands) {
					brandItem = brands[j];
					hot = brandItem.is_hot;
					if(hot == true) {
						brandItemDom = $('<li data-brand-id="' + brandItem.code + '">' + brandItem.chinese_name + '</li>');
						brandItemDom.bind("click", {
							code: brandItem.code
						}, function(e) {
							$("[name=code]").val(e.data.code);
							$(".popup-mask2").hide();
							$(".country-pop").hide();
						});
						$(".brand-hot-dom").append(brandItemDom);
					}

					if(!firstKeySet) {
						tempId = "brand-key-" + i;
						classifyDom = $('<ul><h2 id="' + tempId + '">' + i + '</h2></ul>');
						brandPopUl.append(classifyDom);
						brandLetter.append('<a href="#' + tempId + '">' + i + '</a>');
						firstKeySet = true;
					}
					brandItemDom = $('<li data-brand-id="' + brandItem.code + '">' + brandItem.chinese_name + '</li>');
					classifyDom.append(brandItemDom);

					brandItemDom.bind("click", {
						code: brandItem.code
					}, function(e) {
						$("[name=code]").val(e.data.code);
						$(".country-pop").hide();
						$(".popup-mask2").hide()
					});
				}
			}
		}
	});
	
	$("#groupCode").on("click", function(e) {
		$(".country-pop").show();
		$(".popup-mask2").show()
	})
	
	$(".countryClose").on("click", function(e) {
	    $(".country-pop").hide();
	    $(".popup-mask2").hide()
	})
	
	$('#modalFormTipClose').on('click', function() {
		$('.popup-mask2').hide();
		$('.modalFormTip').hide()
	})

	$(".submitMessage").on("click", function (e) {
		var isEmail = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/; 
		var vcodePhone = $("[name=code]").val();
		var telNumber = $("[name=telNumber]").val();
		if(telNumber == ''){
			inputTip('手机号码不能为空');
			return false;
		}
		
		var emallAddress = $("[name=emallAddress]").val();
		if(emallAddress == '' || !(isEmail.test(emallAddress))){
			inputTip('请输入正确的邮箱');
			return false;
		}
		
		var passwordInput = $("[name=password1]").val();
		if(passwordInput == ''){
			inputTip('请输入密码');
			return false;
		}
		
		var info = JSON.stringify({phone: telNumber, countryCode: vcodePhone, email: emallAddress, password: passwordInput})
		$.ajax({
			url: 'http://vip.fashionfinger.com/shop/api/v2/user/apply',
			type : "POST",
    		contentType: "application/json",
    		data: JSON.stringify({phone: telNumber, countryCode: vcodePhone, email: emallAddress, password: passwordInput}),
    		success:function (data) {
	            if (0 == data.code) {
	            	$('.inputTip').hide();
	                $('.modalFormTip').show();
	                $(".popup-mask2").show();
	            } else {
	            	inputTip(data.msg);
	            }
            }
		})
	})
})